define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index' + location.search,
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                $('#table .editalbe-balance').editable({
                    type: 'text',
                    name: 'score',
                    url: 'user/user/setField',
                    savenochange: true,
                    title: '修改积分余额：正数为增加，负数为减少',
                    validate: function (value) {
                        value = parseFloat(value);
                        var res = /^-?([1-9]\d*|0)?$/.test(value);

                        return res ? {
                            newValue: value
                        } : '必须为整数';
                    },
                    success: function (response, newvalue) {
                        if (response.code === 0) {
                            return response.msg;
                        } else {
                            return {
                                newValue: (Number($(this).editable('getValue', true)) + Number(newvalue)).toFixed(0)
                            };
                        }
                    }
                });

                $('#table .editalbe-balance').on('shown', function(e, editable) {
                    editable.input.$input.val('');
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'parent_id', title:'上级ID', sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true, formatter: function (value, row, index) {
                                return  '<a href="javascript:;" class="editalbe-balance"   data-pk="'+row.id+'">' + value+ '</a>';
                            }},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), width: '200px',table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'address',
                                    text: '地址管理',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-gg-circle',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'leescore/address/index?uid={ids}'
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});